SUBROUTINE vfunapprox_age_theta_q(agenin,agemayorin,emaxin,betain)
  USE Commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'

  INTEGER, INTENT(IN)  :: agenin, agemayorin(agenin)
  REAL(8), INTENT(IN)  :: emaxin(Ntheta_vot,Nwealth,agenin,Nq)
  REAL(8), INTENT(OUT) :: betain(MMpmnew_age)
  REAL(8), ALLOCATABLE :: y(:), xvar(:,:), res(:,:,:,:), vfunout(:,:,:,:), ones(:), xpx(:,:), xpy(:), invxx(:,:), res_final(:), work(:), betastart(:)
  REAL(8)              :: Rsquared, dble_agemayorin(agenin), av_res(Nwealth), av_res_temp(Nwealth), v_slope, v_slope_p
  INTEGER              :: ii, i, j, iwealth, info, itheta_vot, MM_t, agei, l_wealth, lwork, iq
  CHARACTER :: uplo='U'

  ii = Ntheta_vot*Nwealth*agenin*Nq
  dble_agemayorin(:) = DBLE(agemayorin(:))

  ALLOCATE(y(ii), res_final(ii), res(Ntheta_vot,agenin,Nwealth,Nq), ones(ii), vfunout(Ntheta_vot,agenin,Nwealth,Nq))
  y = 0d0; res = 0d0

  MM_t = 1+Npol_age+Npol_theta_vot+Npol_q
  ALLOCATE(xvar(ii,MM_t),xpx(MM_t,MM_t), xpy(MM_t), invxx(MM_t,MM_t), betastart(MM_t))

  xpx = 0d0; xpy = 0d0
  betastart = 0d0
  betain = 0d0

  !Average the residuals for each iwealth
  av_res_temp = 0d0
  DO iwealth = 1, Nwealth
     DO agei = 1, agenin
        DO itheta_vot = 1, Ntheta
           DO iq = 1, Nq

              av_res_temp(iwealth) = av_res_temp(iwealth) + emaxin(itheta_vot,iwealth,agei,iq)

           END DO
        END DO
     END DO
  END DO
  av_res_temp = av_res_temp/DBLE(Ntheta_vot*agenin*Nq)

  !Compute the residuals V - (alpha_1+alpha_2*q+alpha_3*q^2)
  DO iwealth = 1, Nwealth
     DO agei = 1, agenin
        DO itheta_vot = 1, Ntheta
           DO iq = 1, Nq

              res(itheta_vot,agei,iwealth,iq) = emaxin(itheta_vot,iwealth,agei,iq) - av_res_temp(iwealth)

           END DO
        END DO
     END DO
  END DO

  !We compute the coefficients on public consumption in V = f(sav)+alpha_1+alpha_2*q+alpha_3*q^2
  ones = 1d0
  i = 0
  DO iwealth = 1, Nwealth
     DO agei = 1, agenin
        DO itheta_vot = 1, Ntheta
           DO iq = 1, Nq
      
              i = i + 1
              y(i) = res(itheta_vot,agei,iwealth,iq)
              xvar(i,1:MM_t) = (/ ones(i), dble_agemayorin(agei), dble_agemayorin(agei)**2d0, theta(itheta_vot), qconsvec_g(iq), DBLE(itheta_vot-1)*qconsvec_g(iq) /)

!!$              IF (flag_here == 260) write(*,'(a,4i4,4f14.8)') 'y inside', itheta_vot,agei,iwealth,iq,y(i),emaxin(itheta_vot,iwealth,agei,iq),av_res_temp(iwealth)
              
           END DO
        END DO
     END DO
  END DO
  
  xpx = MATMUL(TRANSPOSE(xvar(:,:)),xvar(:,:))/SIZE(y)
  xpy = MATMUL(TRANSPOSE(xvar(:,:)),y(:))/SIZE(y)
     
  invxx = 0d0
  DO j=1, MM_t
     invxx(j,j)=1d0
  END DO
  
  LWORK = MM_t
  IF (ALLOCATED(WORK)) DEALLOCATE(WORK)
  ALLOCATE(WORK(LWORK))
  CALL DPOSV(uplo,MM_t,MM_t,xpx,MM_t,invxx,MM_t,info)
  IF (info /= 0) THEN
     DO j = 1, ii
        WRITE(*,'(a,1i3,7f12.6)') 'y,x',j,y(j),xvar(j,1:MM_t)
     ENDDO
     DO iwealth = 1, Nwealth
        DO agei = 1, agenin
           DO itheta_vot = 1, Ntheta
              DO iq = 1, Nq

                 WRITE(*,'(a,3i4,2f12.6)') 'emaxin', itheta_vot,iwealth,agei,iq, emaxin(itheta_vot,iwealth,agei,iq), av_res_temp(iwealth)

              END DO
           END DO
        END DO
     END DO
     WRITE(*,*) 'MATRIX COULD NOT INVERT vfun_age 1', 'info', info
  END IF
  
  betastart(:) = MATMUL(invxx,xpy)

  IF (flag_Tend == 1) THEN
     v_slope_p = (av_res_temp(2)-av_res_temp(1))/(wealth(2) - wealth(1))
     av_res(Nwealth) = av_res_temp(Nwealth)
     DO iwealth = Nwealth-1, 1, -1
        IF (av_res_temp(iwealth+1) - av_res_temp(iwealth) > small_no) THEN
           av_res(iwealth) = av_res_temp(iwealth)
           l_wealth = iwealth
           V_slope = (av_res_temp(iwealth+1)-av_res_temp(iwealth))/(wealth(iwealth+1) - wealth(iwealth))
        ELSE        
           V_slope = inc_slope*V_slope_p+V_slope_p
           av_res(iwealth) = V_slope*(wealth(iwealth) - wealth(iwealth+1)) + av_res(iwealth+1)

           IF (flag_here == 9823) THEN
              IF (av_res(iwealth+1) - av_res(iwealth) < 0d0) THEN
                 write(*,'(a,1i4,5f15.7)') 'beta vfunapprox_age 1', iwealth, av_res(iwealth), V_slope, V_slope_p, av_res(iwealth+1), wealth(iwealth) - wealth(iwealth+1)              
                 write(*,'(a,10f15.7)') 'beta vfunapprox_age 2', av_res(:)
              END IF
           END IF

        END IF
        V_slope_p = V_slope
     END DO

!!$     IF (flag_here == 9823) write(*,'(a,10f15.7)') 'beta vfunapprox_age', av_res(:)

  ELSE
     av_res = av_res_temp
  END IF
!!$  betain(:) = (/ av_res(:), betastart(:) /)
  betain(1:Nwealth+MM_t-Npol_q) = (/ av_res(:), betastart(1:MM_t-Npol_q) /)
  betain(Nwealth+MM_t-Npol_q+Npol_st+1:Nwealth+MM_t+Npol_st) = betastart(MM_t-Npol_q+1:MM_t)

!!$  IF (flag_here == 260) THEN
!!$     write(*,'(a,18f14.8)') 'betain try', betain
!!$     write(*,'(a,18f14.8)') 'betastart', betastart
!!$  END IF
  
  !Compare the actual value function with the approximation
  i = 0
  DO iwealth = 1, Nwealth
     DO agei = 1, agenin
        DO itheta_vot = 1, Ntheta
           DO iq = 1, Nq
              
              i = i + 1
              vfunout(itheta_vot,agei,iwealth,iq) = av_res(iwealth) + betastart(1) + betastart(2)*dble_agemayorin(agei)+betastart(3)*dble_agemayorin(agei)**2d0 + betastart(4)*theta(itheta_vot) + betastart(5)*qconsvec_g(iq) + betastart(6)*DBLE(itheta_vot-1)*qconsvec_g(iq)
              res_final(i) = emaxin(itheta_vot,iwealth,agei,iq) - vfunout(itheta_vot,agei,iwealth,iq)
              y(i) = emaxin(itheta_vot,iwealth,agei,iq)
              
           END DO
        END DO
     END DO
  END DO

  Rsquared = (SUM((y-SUM(y)/DBLE(SIZE(y)))**2d0) - SUM(res_final**2d0)) / SUM((y-SUM(y)/DBLE(SIZE(y)))**2d0)

  IF (Rsquared < 0.5d0 .AND. flag_Tend == 0) THEN
!!$  IF (Rsquared < 0.75d0) THEN
!!$  IF (flag_here == 1) THEN
     write(*,*) 'Rsquared vfunapprox_age_theta_q', Rsquared
     DO iwealth = 1, Nwealth
        DO agei = 1, agenin           
           DO itheta_vot = 1, Ntheta
              DO iq = 1, Nq

                 write(*,'(a,4i4,5f20.10)') 'vfunapprox_age_theta_q', itheta_vot,agei,iwealth,iq,emaxin(itheta_vot,iwealth,agei,iq),vfunout(itheta_vot,agei,iwealth,iq),av_res(iwealth) + betastart(1) + betastart(2)*dble_agemayorin(agei)+betastart(3)*dble_agemayorin(agei)**2d0 + betastart(4)*theta(itheta_vot) + betastart(5)*theta(itheta_vot)*qconsvec_g(iq) + betastart(6)*theta(itheta_vot)*DBLE(itheta_vot-1)*qconsvec_g(iq), av_res(iwealth) + betastart(5)*qconsvec_g(iq) + betastart(6)*qconsvec_g(iq)**2d0,betastart(1) + betastart(2)*dble_agemayorin(agei)+betastart(3)*dble_agemayorin(agei)**2d0 + betastart(4)*theta(itheta_vot) + betastart(5)*theta(itheta_vot)*qconsvec_g(iq) + betastart(6)*theta(itheta_vot)*DBLE(itheta_vot-1)*qconsvec_g(iq)

              END DO
           END DO
        END DO
     END DO
  END IF

  DEALLOCATE(y,xvar,res,vfunout)

END SUBROUTINE vfunapprox_age_theta_q
